package com.example.drugaiservice.controller;


import com.example.drugaiservice.common.result.Result;
import com.example.drugaiservice.pojo.dto.HospDTO;
import com.example.drugaiservice.pojo.dto.NameDTO;
import com.example.drugaiservice.pojo.entity.Drug;
import com.example.drugaiservice.pojo.entity.Hosp;
import com.example.drugaiservice.pojo.vo.HospVO;
import com.example.drugaiservice.service.AskService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/ask")
public class AskController {
    @Autowired
    AskService askService;
    @Autowired
    private RedisTemplate redisTemplate;

    //此接口为输入药品大概名称返回药品信息
    @PostMapping("/search")
    public Result<Drug> search(@RequestBody NameDTO name) throws JsonProcessingException, UnsupportedEncodingException {
        log.info(name.getName());
        return Result.success(askService.NameReturnDrug(name.getName()));
    }
    //
    @PostMapping("/top2")
    public Result<List<HospVO>> top2(@RequestBody HospDTO hosp){
        return Result.success(askService.findTop2(hosp));
    }


    //此接口为测试大模型问答接口，不会用于前端
    @PostMapping("/ask")
    public Result ask(@RequestBody String question) throws Exception {
        return Result.success(askService.askS(question));
    }
    //此接口用于清空redis的数据，仅测试用
    @PostMapping("/clear")
    public Result clearRedis(){
        Set<String> keys = redisTemplate.keys("*");
        int i=1;
        if (keys != null) {
            redisTemplate.delete(keys);
            log.info(i++ +"已被清理");
        }
        return Result.success();
    }


}
